package com.hejia.bulbul.controller;

import java.util.HashMap;

import com.hejia.alauda.entity.SystemMessage;
import com.hejia.alauda.entity.example.SystemMessageExample;
import com.hejia.alauda.service.MessageService;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.DataTablesReturnObject;
import com.hejia.bulbul.entity.JSONParam;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 
 * 
 *
 *
 * Created by showone on 2014 上午11:19:18.
 *
 * @Copyright: 2014 www.hejiajinrong.com All rights reserved.
 */

@Controller
@RequestMapping("message")
public class MessageController extends BaseController {
	@Resource
	private MessageService messageService;

	/**
	 *
	 * @param pager
	 *            分页信息
	 * @param model
	 *            设置参数的model
	 * @return list页面
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		messageService.setSystemMessageDead();
		return "/message/list";
	}

	/**
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		return "/message/add";
	}

	/**
	 *
	 * @param product
	 * @param protocolIds
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(SystemMessage systemMessage, ModelMap model,
			RedirectAttributes redirectAttributes) {
		if (messageService.sendSystemMessage(systemMessage)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "系统消息添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "系统消息添加失败");
		}

		return "redirect:/message/list";
	}

	/**
	 * 系统消息删除
	 *
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String delete(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (messageService.deleteSystemMessage(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "系统消息删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "系统消息删除失败");
		}

		return "redirect:/message/list";
	}

	/**
	 * 消息修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String show(@PathVariable Long id, ModelMap model) {
		SystemMessage msg = messageService.getSystemMessage(id);
		model.put("message", msg);

		return "/message/add";
	}

	/**
	 * 系统消息修改提交
	 *
	 * @param msg
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(SystemMessage msg, ModelMap model,
			RedirectAttributes redirectAttributes) {
		
		SystemMessage oldMsg = messageService.getSystemMessage(msg.getId());
		
		oldMsg.setCategory(msg.getCategory());
		oldMsg.setTitle(msg.getTitle());
		oldMsg.setContent(msg.getContent());
		oldMsg.setUrl(msg.getUrl());
		oldMsg.setOpenAppPage(msg.getOpenAppPage());
		oldMsg.setDeadline(msg.getDeadline());

		if (messageService.updateSystemMessage(oldMsg)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "系统消息修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "系统消息修改失败");
		}
		return "redirect:/message/list";
	}

	@RequestMapping(value = "publish/{id}", method = RequestMethod.GET)
	public String publish(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (messageService.publishSystemMessage(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "系统消息发布成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "系统消息发布失败");
		}

		return "redirect:/message/list";
	}

	/**
	 * 以ajax格式返回列表请求
	 *
	 * @param params
	 * @return
	 */

	@RequestMapping(value = "listAsAjax", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesReturnObject<SystemMessage> listAsAjax(
			@RequestBody JSONParam[] params) {
		HashMap<String, String> paramMap = JSONParam.convertToMap(params);

		// 分页信息
		Pager<SystemMessage> pager = new Pager<SystemMessage>();
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		pager.setPageSize(length);
		pager.setPageNumber(start / length + 1);

		// 查询条件
		SystemMessageExample exp = new SystemMessageExample();

		String sSearch = paramMap.get("sSearch");
		if (StringUtils.isNotEmpty(sSearch)) {
			exp.appendCriterion("name like  ", "%" + sSearch + "%");
		}
		// 排序
		int iSortingCols = Integer.parseInt(paramMap.get("iSortingCols"));
		for (int i = 0; i < iSortingCols; i++) {
			String iSortCol = paramMap.get("iSortCol_" + i);
			String mDataProp = paramMap.get("mDataProp_" + iSortCol);
			String sSortDir = paramMap.get("sSortDir_" + i);
			if (i > 0) {
				exp.setOrderByClause(exp.getOrderByClause() + ",");
				exp.setOrderByClause(exp.getOrderByClause() + mDataProp + " "
						+ sSortDir);
			} else {
				exp.setOrderByClause(mDataProp + " " + sSortDir);
			}
		}

		pager = messageService.findSystemMessage(exp, pager);

		DataTablesReturnObject<SystemMessage> dtr = new DataTablesReturnObject<SystemMessage>(
				pager.getTotalCount(), paramMap.get("sEcho"), pager.getList());
		return dtr;
	}

}
